package com.ordering.dao.addr.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.ordering.constant.ResultResponse;
import com.ordering.dao.addr.AddrDao;
import com.ordering.entity.Addr;
import com.ordering.util.DateAndTime;
import com.ordering.util.HibernateUtil;

public class AddrDaoImpl implements AddrDao{
	
	private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
	
	
	@Override
	public int add(Addr addr) throws Exception {
		Session s=null;
        ResultResponse resultResponse = new ResultResponse();
        try{
            s=HibernateUtil.getSession();
            
            Transaction tran = s.beginTransaction();
            //设置创建时间
            addr.setCreateTime( DateAndTime.getCurrentTime());
            s.save(addr);
            tran.commit();
            return 1;
        }catch (Exception e) {
            resultResponse.setMessage(e.getMessage());
            e.printStackTrace();
        }finally{
            if(s!=null)
                s.close();
        }
        return 0;
	}

	@Override
	public int delect(Addr addr) throws Exception {
		Session s=null;
        try
        {
            s=HibernateUtil.getSession();
            Transaction tran = s.beginTransaction();
            s.delete(addr);
            tran.commit();
            return 1;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(s!=null)
                s.close();
        }
        return 0;
	}

	@Override
	public List<Addr> getAll(Addr addr) throws Exception {
		Session s=null;
        s = HibernateUtil.getSession();
        
        //根据orderItem带来的参数拼接查询条件
        StringBuffer sb = new StringBuffer("from Addr where 1=1 ");
        //状态
        if(addr.getStatus() > 0){
            sb.append(" and status=? ");
        }
        //用户id
        if(addr.getUserId() > 0){
            sb.append(" and userId=? ");
        }

        sb.append(" order by create_time desc ");
        
        Query q = s.createQuery(sb.toString());
        
        int i = 0;
        //状态
        if(addr.getStatus() > 0){
            q.setInteger(i++, addr.getStatus());
        }
        //用户id
        if(addr.getUserId() > 0){
            q.setInteger(i++, addr.getUserId());
        }


        List<Addr> list = q.list(); 
        return list;
	}

	@Override
	public boolean updateStatus(Addr addr) throws Exception {
		Session s=null;
        s=HibernateUtil.getSession();
        StringBuffer sb = new StringBuffer("update Addr set status = ? ");

        //根据id更行
        sb.append(" where id = ? ");
        
        Query q = s.createQuery(sb.toString());
        
        //插入状态
        q.setInteger(0, addr.getStatus());
      
        //插入id
        q.setInteger(1, addr.getId());
        	
        boolean result = q.executeUpdate() > 0;
        
        if(s != null)
        s.close();
        return result;
	}

}
